package cn.net.yugu.doraemon.common.dal.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import lombok.Data;

/**
 * @author xiangzr
 * @version 2023/4/1 10:41:03
 */
@Data
public class Sort implements Serializable {

    protected static final Direction DIRECTION_DEFAULT = Direction.ASC;
    private List<Order> orders = new ArrayList<>();

    public static Sort of(String property) {
        return of(property, DIRECTION_DEFAULT);
    }

    public static Sort of(String property, Direction direction) {
        final Sort sort = new Sort();
        sort.add(property, direction);
        return sort;
    }

    public Sort add(Order order) {
        orders.add(order);
        return this;
    }

    public Sort add(String property) {
        return add(new Order(property));
    }

    public Sort add(String property, Direction direction) {
        return add(new Order(property, direction));
    }

    @Data
    public static class Order implements Serializable {

        private final String direction;
        private final String property;

        public Order(String property, Direction direction) {
            this.property = property;
            this.direction = direction.name();
        }

        public Order(String property) {
            this.property = property;
            this.direction = DIRECTION_DEFAULT.name();
        }
    }

    public enum Direction {
        ASC, DESC;
    }
}
